import matplotlib.pyplot as plt
from alg import distribute_cookies
plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置中文字体为黑体
plt.rcParams['axes.unicode_minus'] = False    # 正常显示负号

def visualize_distribution(cookies, k):
    # 暴力分配，获得最优分配方案
    best = [float('inf')]
    best_children = []

    def backtrack(i, children):
        if i == len(cookies):
            unfair = max(children)
            if unfair < best[0]:
                best[0] = unfair
                best_children.clear()
                best_children.extend(children[:])
            return
        for j in range(k):
            children[j] += cookies[i]
            backtrack(i + 1, children)
            children[j] -= cookies[i]
            if children[j] == 0:
                break

    backtrack(0, [0]*k)
    plt.bar(range(1, k+1), best_children)
    plt.xlabel('孩子编号')
    plt.ylabel('获得的饼干数')
    plt.title(f'最小不公平度分配: {best[0]}')
    plt.show()

if __name__ == "__main__":
    cookies = [8, 15, 10, 20, 8]
    k = 2
    visualize_distribution(cookies, k)